package com.nuanshui.heatedloan.credit.spring;

import com.nuanshui.framework.lang.ClassUtil;
import com.nuanshui.framework.springContext.SpringContextUtil;
import com.nuanshui.heatedloan.credit.annotation.CustomAuthAnnotation;
import com.nuanshui.heatedloan.credit.bean.CustomAuthHandlerMeta;
import com.nuanshui.heatedloan.credit.facade.ExecuteCustomAuthService;
import com.nuanshui.heatedloan.credit.handler.CustomAuthHandler;
import com.nuanshui.heatedloan.credit.service.ExecuteCustomAuthServiceImpl;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Created by wangxw on 2017/6/26.
 */

@Component
public class CustomAuthExecuteFactoryBean implements BeanFactoryAware, BeanPostProcessor, FactoryBean<ExecuteCustomAuthService> {

    private BeanFactory beanFactory=null;

    private ExecuteCustomAuthServiceImpl executeCustomAuthService=new ExecuteCustomAuthServiceImpl();

    private volatile boolean isInitEd=false;   //是否初始化过

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
       this.beanFactory=beanFactory;
    }

    @Override
    public ExecuteCustomAuthService getObject() throws Exception {
        return executeCustomAuthService;
    }

    @Override
    public Class<?> getObjectType() {
        return ExecuteCustomAuthService.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        CustomAuthAnnotation customAuthAnnotation = AnnotationUtils.getAnnotation(targetClass, CustomAuthAnnotation.class);
        if (customAuthAnnotation != null && bean instanceof CustomAuthHandler) {
            if (!isInitEd) {
                synchronized (customAuthAnnotation) {
                    if (!isInitEd) {
                        isInitEd = true;
                        Set<Class<?>> authHandlersClass = ClassUtil.getAnnotationClasses(CustomAuthAnnotation.class,"com.nuanshui.heatedloan.credit.handler.impl");
                        for (Class<?> authHandlerClass : authHandlersClass) {
                            CustomAuthAnnotation customAuthAnnotationTemp = authHandlerClass.getAnnotation(CustomAuthAnnotation.class);
                            if (customAuthAnnotationTemp != null) {
                                CustomAuthHandlerMeta customAuthHandlerMeta = new CustomAuthHandlerMeta();
                                customAuthHandlerMeta.setOrder(customAuthAnnotationTemp.order());
                                customAuthHandlerMeta.setCustomAuthHandler((CustomAuthHandler) SpringContextUtil.getBean(authHandlerClass));
                               // executeCustomAuthService.getCustomAuthExecuteRegister().regisgerCustomAuthHandler(customAuthHandlerMeta);
                            }
                        }
                    }
                }
            }

        }
        return bean;
    }
}
